package com.lry.thompson;

import java.util.*;

public class MiniDfaManager {

    private static int GROUP_COUNT = 0;
    private static Map<Integer,MiniDfa> miniDfaMap = new HashMap<>();
    private static List<MiniDfa> miniDfas = new ArrayList<>();

    public static MiniDfa start;

    public static MiniDfa createMiniDfa(){
        MiniDfa miniDfa = new MiniDfa(GROUP_COUNT);
        miniDfaMap.put(GROUP_COUNT,miniDfa);
        miniDfas.add(miniDfa);
        GROUP_COUNT++;
        return miniDfa;
    }

    public static List<MiniDfa> getMiniDfas(){
        return miniDfas;
    }


    public static MiniDfa getByDfaStateNum(int dfaStateNum){
        for (MiniDfa miniDfa : miniDfas) {
            if(containsDfa(miniDfa,dfaStateNum)){
                return miniDfa;
            }
        }
        return null;
    }

    private static boolean containsDfa(MiniDfa miniDfa,int dfaStateNum){
        return null!=miniDfa.getDfaByStateNum(dfaStateNum);
    }

    public static boolean isEnd(int groupNum) {
        MiniDfa miniDfa = miniDfaMap.get(groupNum);
        if(null==miniDfa){
            return false;
        }
        return miniDfa.isEnd();
    }
}
